/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ingenieriaeconomica;
import java.text.DecimalFormat;
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.ArrayRealVector;
import org.apache.commons.math3.linear.DecompositionSolver;
import org.apache.commons.math3.linear.LUDecomposition;
import org.apache.commons.math3.linear.RealVector;
/**
 *
 * @author Lisi
 */
public class Pronosticos extends javax.swing.JFrame {

    /**
     * Creates new form Pronosticos
     */    
    private int _elegido;
    private double _z;
    public double[] _ls,_li;
    private DecimalFormat FD2 = new DecimalFormat("#######.##");
    private DecimalFormat FD4 = new DecimalFormat("#######.####");
    int n,k;
    double[] _x,_y,_xy,_x2,_y2,_noexplicada,_x2y,_x3,_x4,_total,_explicada;
    double[] _lnx,_lnx2,_lnxy,_lny,_lny2,_lnxlny,_xlny,_sxy,_r,_r2;
    double[][] _yprima;
    double _ypromedio;
    String[] _formula;
    public boolean _limSel;
    public Principal princi;
    
    public Pronosticos() {
        initComponents();
    }
    
    public void FormPri(Principal este)
    {
        princi=este;
    }
    
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        _Y = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        _XY = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        _Resumen = new javax.swing.JTable();
        _EtiquetaIdeal = new javax.swing.JLabel();
        _EtiquetaFormula = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        _X = new javax.swing.JTable();
        jScrollPane5 = new javax.swing.JScrollPane();
        _Limites = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        _radio90 = new javax.swing.JRadioButton();
        _radio95 = new javax.swing.JRadioButton();
        _radio99 = new javax.swing.JRadioButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Modulo de Pronosticos");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        _Y.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Y"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Double.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        _Y.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(_Y);

        _XY.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "X", "Y"
            }
        ));
        _XY.setAutoscrolls(false);
        jScrollPane2.setViewportView(_XY);

        jButton1.setText("Pronosticar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        _Resumen.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"r", null, null, null, null, null},
                {"r^2", null, null, null, null, null},
                {"Sxy", null, null, null, null, null}
            },
            new String [] {
                "", "Lineal", "Polinomial", "Exponencial", "Potencial", "Logaritmico"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true, true, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(_Resumen);

        _EtiquetaIdeal.setText("Modelo Ideal: ");

        _EtiquetaFormula.setText("Y=3xa+2b");

        _X.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "X"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        _X.getTableHeader().setReorderingAllowed(false);
        _X.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                _XFocusGained(evt);
            }
        });
        jScrollPane4.setViewportView(_X);
        _X.getColumnModel().getColumn(0).setHeaderValue("X");

        _Limites.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Limite Superior", "Limite Inferior"
            }
        ));
        _Limites.setAutoscrolls(false);
        jScrollPane5.setViewportView(_Limites);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Confianza"));

        buttonGroup1.add(_radio90);
        _radio90.setText("90% (1.65)");
        _radio90.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _radio90ActionPerformed(evt);
            }
        });

        buttonGroup1.add(_radio95);
        _radio95.setText("95% (1.96)");
        _radio95.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _radio95ActionPerformed(evt);
            }
        });

        buttonGroup1.add(_radio99);
        _radio99.setText("99% (2.58)");
        _radio99.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _radio99ActionPerformed(evt);
            }
        });

        buttonGroup2.add(jRadioButton1);
        jRadioButton1.setText("Limite Superior");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        buttonGroup2.add(jRadioButton2);
        jRadioButton2.setText("Limite Inferior");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton1)
                    .addComponent(_radio90)
                    .addComponent(_radio95)
                    .addComponent(_radio99)
                    .addComponent(jRadioButton2))
                .addContainerGap(16, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(_radio99, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(_radio95)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(_radio90, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton2)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(_EtiquetaIdeal)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(_EtiquetaFormula))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(19, 19, 19)
                        .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 511, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(_EtiquetaIdeal)
                    .addComponent(_EtiquetaFormula))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void Contar()
    {
        k=2;
        for(int i=0;i<15;i++)
        {
            if(_Y.getValueAt(i, 0)==null)
            {
                n=i;
                break;
            }
            _X.setValueAt(i+1, i, 0);
        }
        for(int i=0;i<10;i++)
        {
            _XY.setValueAt(i+1+n, i, 0);
        }
    }
    
    private void ModeloLineal()
    {
        //Modelo Lineal
        // _sxy,_r,_r2 _yprima[5][10]_ypromedio String ylin,ypol,yrcp,ypot,ylog;
        double a=0.0;
        double b=0.0;
        a=((n*_xy[n])-(_x[n]*_y[n]))/((n*_x2[n])-(_x[n]*_x[n]));
        b=(_y[n]/n)-(a*_x[n]/n);
        _formula[0]= FD2.format(a)+"X + "+FD2.format(b);
        for(int i=0;i<10;i++)
        {
            _yprima[0][i]=a*(i+n+1)+b;
            _yprima[0][10]+=_yprima[0][i];
        }
        _yprima[0][11]=_yprima[0][10]/10;
        _r[0]=((n*_xy[n])-(_x[n]*_y[n]))/Math.sqrt(((n*_x2[n])-(_x[n]*_x[n]))*((n*_y2[n])-(_y[n]*_y[n])));
        _r2[0]=_r[0]*_r[0];
        _noexplicada[n]=0;
        for(int i=0;i<n;i++)
        {
            _noexplicada[i]=(_y[i]-(a*(i+1)+b))*(_y[i]-(a*(i+1)+b));
            _noexplicada[n]+=_noexplicada[i];
        }
        _noexplicada[n+1]=_noexplicada[n]/(n);
        _sxy[0]=Math.sqrt(_noexplicada[n]/(n-k));
    }
    
    private void ModeloPolinomial()
    {
        double a;
        double b;
        double c;
        double[][] matriz = new double[3][3];
        matriz[0][0]=n;
        matriz[0][1]=_x[n];
        matriz[0][2]=_x2[n];
        matriz[1][0]=_x[n];
        matriz[1][1]=_x2[n];
        matriz[1][2]=_x3[n];
        matriz[2][0]=_x2[n];
        matriz[2][1]=_x3[n];
        matriz[2][2]=_x4[n];
        DecompositionSolver solver = new LUDecomposition(new Array2DRowRealMatrix(matriz,false)).getSolver();
        RealVector cons = new ArrayRealVector(new double[] {_y[n],_xy[n],_x2y[n] }, false);
        RealVector solution = solver.solve(cons);
        c = solution.getEntry(0);
        b = solution.getEntry(1);
        a = solution.getEntry(2);
        _formula[1]= FD2.format(a)+"X^2 + "+FD2.format(b)+"X + "+FD2.format(c);
        for(int i=0;i<10;i++)
        {
            _yprima[1][i]=(a*((i+n+1)*(i+n+1)))+(b*(i+n+1))+c;
            _yprima[1][10]+=_yprima[1][i];
        }
        _yprima[1][11]=_yprima[1][10]/10;   
        _noexplicada[n]=0;
        _explicada[n]=0;
        for(int i=0;i<n;i++)
        {
            _noexplicada[i]=(_y[i]-(a*(i+1)*(i+1)+b*(i+1)+c))*(_y[i]-(a*(i+1)*(i+1)+b*(i+1)+c));
            _noexplicada[n]+=_noexplicada[i];
            _explicada[i]=((a*(i+1)*(i+1)+b*(i+1)+c)-_ypromedio)*((a*(i+1)*(i+1)+b*(i+1)+c)-_ypromedio);
            _explicada[n]+=_explicada[i];
        }
        _noexplicada[n+1]=_noexplicada[n]/n;
        _explicada[n+1]=_explicada[n]/n;
        _r[1]=Math.sqrt(_explicada[n]/_total[n]);
        _r2[1]=_r[1]*_r[1];
        _sxy[1]=Math.sqrt(_noexplicada[n]/(n-k));
    }
    
    private void ModeloExponencial()
    {
        double b=(_xlny[n+1]-(_x[n+1]*_lny[n+1]))/(_x2[n+1]-(_x[n+1]*_x[n+1]));
        double a=_lny[n+1]-(b*_x[n+1]);
        double e=2.718281828;
        double A=Math.pow(e, a);
        _formula[2]=FD4.format(A)+"e^("+FD4.format(b)+"X)";
        for(int i=0;i<10;i++)
        {
            _yprima[2][i]=A*(Math.pow(e, b*(i+n+1)));
            _yprima[2][10]+=_yprima[2][i];
        }
        _yprima[2][11]=_yprima[2][10]/10;
        _r[2]=(_xlny[n+1]-(_x[n+1]*_lny[n+1]))/Math.sqrt((_x2[n+1]-(_x[n+1]*_x[n+1]))*(_lny2[n+1]-(_lny[n+1]*_lny[n+1])));
        _r2[2]=_r[2]*_r[2];
        _noexplicada[n]=0;
        for(int i=0;i<n;i++)
        {
            _noexplicada[i]=(_y[i]-(A*Math.pow(e,b*(i+1))))*(_y[i]-(A*Math.pow(e,b*(i+1))));
            _noexplicada[n]+=_noexplicada[i];
        }
        _noexplicada[n+1]=_noexplicada[n]/n;
        _sxy[2]=Math.sqrt(_noexplicada[n]/(n-k));        
    }
    
    private void ModeloPotencial()
    {
        double b=(_lnxlny[n+1]-(_lny[n+1]*_lnx[n+1]))/(_lnx2[n+1]-(_lnx[n+1]*_lnx[n+1]));
        double a=_lny[n+1]-(b*_lnx[n+1]);
        double e=2.718281828;        
        double A=Math.pow(e, a);
        _formula[3]=FD4.format(A)+"X^("+FD4.format(b)+")";
        for(int i=0;i<10;i++)
        {
            _yprima[3][i]=A*(Math.pow((i+n+1), b));
            _yprima[3][10]+=_yprima[3][i];
        }
        _yprima[3][11]=_yprima[3][10]/10;        
        _noexplicada[n]=0;
        for(int i=0;i<n;i++)
        {
            _noexplicada[i]=(_y[i]-(A*Math.pow((i+1),b)))*(_y[i]-(A*Math.pow((i+1),b)));
            _noexplicada[n]+=_noexplicada[i];
        }
        _noexplicada[n+1]=_noexplicada[n]/n;        
        _r[3]=(_lnxlny[n+1]-(_lnx[n+1]*_lny[n+1]))/Math.sqrt((_lnx2[n+1]-(_lnx[n+1]*_lnx[n+1]))*(_lny2[n+1]-(_lny[n+1]*_lny[n+1])));
        _r2[3]=_r[3]*_r[3];
        _sxy[3]=Math.sqrt(_noexplicada[n]/(n-k));
    }
    
    private void ModeloLogaritmico()
    {
        double b=((_lnxy[n+1])-(_lnx[n+1]*_y[n+1]))/(_lnx2[n+1]-(_lnx[n+1]*_lnx[n+1]));
        double a=_y[n+1]-(b*_lnx[n+1]); 
        _formula[4]=FD4.format(b)+" Ln(X) + "+FD4.format(a);
        for(int i=0;i<10;i++)
        {
            _yprima[4][i]=(b*Math.log(i+1+n))+a;
            _yprima[4][10]+=_yprima[4][i];
        }
        _yprima[4][11]=_yprima[4][10]/10;        
        _noexplicada[n]=0;
        for(int i=0;i<n;i++)
        {
            _noexplicada[i]=(_y[i]-(b*Math.log(i+1)+a))*(_y[i]-(b*Math.log(i+1)+a));
            _noexplicada[n]+=_noexplicada[i];
        }
        _noexplicada[n+1]=_noexplicada[n]/n;        
        _r[4]=(_lnxy[n+1]-(_lnx[n+1]*_y[n+1]))/Math.sqrt((_lnx2[n+1]-(_lnx[n+1]*_lnx[n+1]))*(_y2[n+1]-(_y[n+1]*_y[n+1])));
        _r2[4]=_r[4]*_r[4];
        _sxy[4]=Math.sqrt(_noexplicada[n]/(n-k));
    }
       
    private void Llenar()
    {
        _formula=new String[5];
        _yprima = new double[5][12];
        _sxy=new double[5]; 
        _r=new double[5]; 
        _r2=new double[5];        
        _x=new double[n+2];
        _y=new double[n+2];
        _xy = new double[n+2];
        _x2 = new double[n+2];
        _y2 = new double[n+2]; 
        _noexplicada = new double[n+2];
        _x2y = new double[n+2];
        _x3 = new double[n+2];
        _x4 = new double[n+2];
        _total = new double[n+2];
        _explicada= new double[n+2];
        _lnx=new double[n+2];
        _lnx2=new double[n+2];
        _lnxy=new double[n+2];
        _lny=new double[n+2];
        _lny2=new double[n+2];
        _lnxlny=new double[n+2];
        _xlny= new double[n+2];
        _x[n]=_y[n]=_xy[n] = _x2[n] = _y2[n] = _noexplicada[n] = _x2y[n] = _x3[n] = _x4[n] = _total[n] = _explicada[n] =0.0;
        _lnx[n]=_lnx2[n]=_lnxy[n]=_lny[n]=_lny2[n]=_lnxlny[n]=_xlny[n]= 0.0;
        _x[n+1]=_y[n+1]=_xy[n+1] = _x2[n+1] = _y2[n+1] = _noexplicada[n+1] = _x2y[n+1] = _x3[n+1] = _x4[n+1] = _total[n+1] = _explicada[n+1] =0.0;
        _lnx[n+1]=_lnx2[n+1]=_lnxy[n+1]=_lny[n+1]=_lny2[n+1]=_lnxlny[n+1]=_xlny[n+1]= 0.0;
        for(int i=0;i<n;i++)
        {
            //Llenar arreglos y Suma
            _x[i]=Double.parseDouble(_X.getValueAt(i,0).toString());
            _x[n]+=_x[i];
            _y[i]=Double.parseDouble(_Y.getValueAt(i,0).toString());
            _y[n]+=_y[i];
            _xy[i]=_x[i]*_y[i];
            _xy[n]+=_xy[i];
            _x2[i]=_x[i]*_x[i];
            _x2[n]+=_x2[i];
            _y2[i]=_y[i]*_y[i];
            _y2[n]+=_y2[i];
            _x2y[i]=_x2[i]*_y[i];
            _x2y[n]+=_x2y[i];
            _x3[i]=_x2[i]*_x[i];
            _x3[n]+=_x3[i];
            _x4[i]=_x2[i]*_x2[i];
            _x4[n]+=_x4[i];             
            _lnx[i]=Math.log(_x[i]);
            _lnx[n]+=_lnx[i];
            _lnx2[i]=_lnx[i]*_lnx[i];
            _lnx2[n]+=_lnx2[i];
            _lnxy[i]=_lnx[i]*_y[i];
            _lnxy[n]+=_lnxy[i];
            _lny[i]=Math.log(_y[i]);
            _lny[n]+=_lny[i];
            _lny2[i]=_lny[i]*_lny[i];
            _lny2[n]+=_lny2[i];
            _lnxlny[i]=_lnx[i]*_lny[i];
            _lnxlny[n]+=_lnxlny[i];
            _xlny[i]=_x[i]*_lny[i];
            _xlny[n]+=_xlny[i];
        }         
        //Promedios
        _ypromedio=_y[n]/n;
        for(int i=0;i<n;i++)
        {
            _total[i]=(_y[i]-_ypromedio)*(_y[i]-_ypromedio);
            _total[n]+=_total[i];
        }
        _x[n+1]=_x[n]/n;
        _y[n+1]=_y[n]/n;
        _xy[n+1]=_xy[n]/n;
        _x2[n+1]=_x2[n]/n;
        _y2[n+1]=_y2[n]/n;
        _x2y[n+1]=_x2y[n]/n;
        _x3[n+1]=_x3[n]/n;
        _x4[n+1]=_x4[n]/n;   
        _total[n+1]=_total[n]/n;
        _lnx[n+1]=_lnx[n]/n;
        _lnx2[n+1]=_lnx2[n]/n;
        _lnxy[n+1]=_lnxy[n]/n;
        _lny[n+1]=_lny[n]/n;
        _lny2[n+1]=_lny2[n]/n;
        _lnxlny[n+1]=_lnxlny[n]/n;
        _xlny[n+1]=_xlny[n]/n;
        ModeloLineal();
        ModeloPolinomial();
        ModeloExponencial();
        ModeloPotencial();
        ModeloLogaritmico();
    }        
    
    private void Resumir()
    {
        double menor=50000.0;
        for(int i=0;i<5;i++)
        {
            _Resumen.setValueAt(FD4.format(_r[i]),0,i+1);
            _Resumen.setValueAt(FD2.format(_r2[i]*100)+"%",1,i+1);
            _Resumen.setValueAt(FD4.format(_sxy[i]),2,i+1);
            if(_sxy[i]<menor)
            {
                menor=_sxy[i];               
            }
        }
        int iguales=0;
        for(int i=0;i<5;i++)
        {
            if(_sxy[i]==menor)
            {
                iguales++;
            }
        }
        int[] mismosxy= new int[iguales];
        int cnt=0;
        for(int i=0;i<5;i++)
        {
            if(_sxy[i]==menor)
            {                
                mismosxy[cnt]=i;
                cnt++;
            }
        }
        double mayor=-1;        
        for(int i=0;i<iguales;i++)
        {
            if(_r2[mismosxy[i]]>mayor)
            {
                _elegido=mismosxy[i];
                mayor=_r2[mismosxy[i]];
            }
        }
        for(int i =0; i<10;i++)
        {
            _XY.setValueAt(FD2.format(_yprima[_elegido][i]), i, 1);
        }
        String modelo="";
        switch (_elegido)
        {
            case 0:
                {
                    modelo="El modelo ideal es el: Lienal";
                    break;
                }
            case 1:
                {
                    modelo="El modelo ideal es el: Polinomial";
                    break;
                }
            case 2:
                {
                    modelo="El modelo ideal es el: Exponencial";
                    break;
                }
            case 3:
                {
                    modelo="El modelo ideal es el: Potencial";
                    break;
                }
            case 4:
                {
                    modelo="El modelo ideal es el: Logartimico";
                    break;
                }
        }        
        _EtiquetaIdeal.setText(modelo);
        _EtiquetaFormula.setText("y = "+_formula[_elegido]);
        
    }
    
    private void Limites()
    {
        _ls=new double[10];
        _li=new double[10];
        for(int i=0;i<10;i++)
        {
            _ls[i]=_yprima[_elegido][i]+(_z*(_sxy[_elegido]/Math.sqrt(n)));
            _li[i]=_yprima[_elegido][i]-(_z*(_sxy[_elegido]/Math.sqrt(n)));
            
            _Limites.setValueAt(FD2.format(_ls[i]), i, 0);
            _Limites.setValueAt(FD2.format(_li[i]), i, 1);
        }
    }
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        _elegido=-1;
        Contar();
        Llenar();
        Resumir();
        //_limSel=true;
        //_radio95.setSelected(true);
        //jRadioButton1.setSelected(true);
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void _radio99ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__radio99ActionPerformed
        // TODO add your handling code here:
        _z=2.58;
        Limites();
    }//GEN-LAST:event__radio99ActionPerformed

    private void _radio95ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__radio95ActionPerformed
        // TODO add your handling code here:
        _z=1.96;
        Limites();
    }//GEN-LAST:event__radio95ActionPerformed

    private void _radio90ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__radio90ActionPerformed
        // TODO add your handling code here:
        _z=1.65;
        Limites();        
    }//GEN-LAST:event__radio90ActionPerformed

    private void _XFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event__XFocusGained
        // TODO add your handling code here:
        _Y .requestFocus();
    }//GEN-LAST:event__XFocusGained

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        // TODO add your handling code here:
        _limSel=true;
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        // TODO add your handling code here:
        _limSel=false;
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        double poriva=1;
        if(princi.EsBruto())
        {
            poriva=(1/1.12);
        }
        if(_limSel)
        {
            for(int i=0;i<10;i++)
            {
                princi.LlenarTabla(FD2.format(_ls[i]*poriva), i+1);
            }            
        }
        else
        {
            for(int i=0;i<10;i++)
            {
                princi.LlenarTabla(FD2.format(_li[i]*poriva), i+1);
            }
        }
    }//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Pronosticos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Pronosticos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Pronosticos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Pronosticos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Pronosticos().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel _EtiquetaFormula;
    private javax.swing.JLabel _EtiquetaIdeal;
    private javax.swing.JTable _Limites;
    private javax.swing.JTable _Resumen;
    private javax.swing.JTable _X;
    private javax.swing.JTable _XY;
    private javax.swing.JTable _Y;
    private javax.swing.JRadioButton _radio90;
    private javax.swing.JRadioButton _radio95;
    private javax.swing.JRadioButton _radio99;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    // End of variables declaration//GEN-END:variables
}
